import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class P2PFrame extends JFrame implements ActionListener, WindowListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ClSrv prog;
	private JTabbedPane mainPanel;
	private DownloadPanel downloadPanel;
	private SearchPanel searchPanel;
	private SharePanel sharePanel;
	private SettingsPanel settingsPanel;

	public P2PFrame() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(0, 0);
		this.setTitle("P2P");
		this.setSize(600, 600);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addWindowListener(this);
		this.setLayout(null);
		downloadPanel = new DownloadPanel();
		searchPanel = new SearchPanel();
		sharePanel = new SharePanel();
		settingsPanel = new SettingsPanel();
		mainPanel = new JTabbedPane();
		mainPanel.setVisible(true);
		mainPanel.setSize(this.getWidth(), this.getHeight());
		mainPanel.setLocation(0, 0);
		mainPanel.addTab("Downloads", new ImageIcon(
				"graphics/icons/download.png"), downloadPanel);
		mainPanel.addTab("Search", new ImageIcon("graphics/icons/search.png"),
				searchPanel);
		mainPanel.addTab("Share", new ImageIcon("graphics/icons/share.png"),
				sharePanel);
		mainPanel.addTab("Settings", new ImageIcon(
				"graphics/icons/settings.png"), settingsPanel);
		this.add(mainPanel, BorderLayout.CENTER);
		repaint();

		prog = new ClSrv("cfg");
		// ustawienie listenerow i odpalenie wyszukiwania

		updateFileList(getProg().getPeers(), searchPanel.getModel().getData());

		
	}

	public ClSrv getProg() {
		return prog;
	}

	public JTabbedPane getMainPanel() {
		return mainPanel;
	}
	
	public DownloadPanel getDownloadPanel() {
		return downloadPanel;
	}

	public void setDownloadPanel(DownloadPanel downloadPanel) {
		this.downloadPanel = downloadPanel;
	}

	public void updateFileList(Vector<Peer> peers, Object data[][]) {
        /*for(int i=0;i<100; i++) {
        	for(int j=0;j<3;j++)
        	data[i][j] = null;
        }*/
		searchPanel.removeEl();	 
		int n = 0;
		if (peers != null) {
			for (int i = 0; i < peers.size(); i++) {
				if (peers.get(i).getFiles() != null) {
					for (int j = 0; j < peers.get(i).getFiles().size(); j++) {
						data[n][0] = peers.get(i).getFiles().get(j).getName();
						data[n][1] = ""+ peers.get(i).getFiles().get(j).getSize();
						data[n][2] = peers.get(i).getName();
						data[n][3] = ""+peers.get(i).getIP();
						data[n][4] = ""+peers.get(i).getListenPort();
						n++;
					}
				} else {
					System.out.println("wiedzialem, ze to null");
				}
			}

			searchPanel.getModel().setData(data);
			repaint();

		} else {
			// data = null;
			searchPanel.getModel().setData(null);
			searchPanel.removeEl();
			repaint();
		}
	}

	public void findFile(String name, Object data[][]) {
		
		Vector<Peer> peers = prog.getPeers();
		searchPanel.removeEl();
		int n = 0;
		if (peers != null) {
			
			for (Peer peer : peers) {
				Vector<MyFile> files = peer.getFiles();
				for (MyFile file : files) {
					System.out.print(file.getName()+" "+peer.getName()+" "+peer.getIP()+" "+peer.getListenPort());
					if (file.getName().compareTo(name) == 0) {
						data[n][0] = name;
						data[n][1] = ""+file.getSize();
						data[n][2] = peer.getName();
						data[n][3] = ""+peer.getIP();
						data[n][4] = ""+peer.getListenPort();
						n++;
						//searchPanel.getModel().setData(data);
					}
				}
			}
		}
		
		searchPanel.getModel().setData(data);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		prog.getOut().println("exit");
		try {
			prog.getSrvSock().close();
		} catch (IOException f) {
			// TODO Auto-generated catch block
			f.printStackTrace();
		}
		System.exit(0);

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}
